﻿using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;


public class SaveHandle 
{
    private static string logDirPath = "";
    private static string LogKeyPath
    {
        get 
        {
            InitLogDirPath();
            return System.IO.Path.Combine(logDirPath, StringToBase64("LogKeys") + ".sio");
        }
    }

    private static string LogContentPath(string key)
    {
        InitLogDirPath();
        return System.IO.Path.Combine(logDirPath, StringToBase64(key) + ".sio");
    }

    private static void InitLogDirPath()
    {
#if UNITY_ANDROID
#elif UNITY_EDITOR
        if (string.IsNullOrEmpty(logDirPath))
        {
            logDirPath = System.IO.Path.Combine(Application.streamingAssetsPath, "LogDir");
        }
#endif
        if (!System.IO.Directory.Exists(logDirPath))
        {
            System.IO.Directory.CreateDirectory(logDirPath);
        }
    }



    public static void WriteLog(string key,string content)
    {
        // 保存key  将所有旧的log全部读出来重写的方式实现这个功能
        var keys = ReadLogKeys();
        bool isSameKey = false;
        if (keys != null)
        {
            for (int i = 0; i < keys.Length; i++)
            {
                if (keys[i].Equals(key))
                {
                    isSameKey = true;
                    break;
                }
            }
        }
        var keyPath = LogKeyPath;
        var dirPath = System.IO.Path.GetDirectoryName(keyPath);
        if (!isSameKey) // 不过有相同的key就不再存储
        {
            List<string> strList = new List<string>();
            if (keys != null) strList.AddRange(keys);
            strList.Add(key);

            // 加密
            var keyContent = "";
            for (int i = 0; i < strList.Count; ++i)
            {
                var str = strList[i];
                keyContent = keyContent + str + "\n";
                strList[i] = StringToBase64(str);
            }
            if (!System.IO.Directory.Exists(dirPath)) System.IO.Directory.CreateDirectory(dirPath);
            System.IO.File.WriteAllLines(keyPath, strList.ToArray());

            Debug.Log("key_ path:" + keyPath + "\n" + keyContent);
        }

        // 保存Log
        var contentPath = LogContentPath(key);
        dirPath = System.IO.Path.GetDirectoryName(contentPath);
        if (!System.IO.Directory.Exists(dirPath))
        {
            System.IO.Directory.CreateDirectory(dirPath);
        }
        var contents = ReadLogContent(key);
        List<string> tStrList = new List<string>();

        // 加密
        var contentStr = "";
        if (contents != null) tStrList.AddRange(contents);
        tStrList.Add(content);
        for (int i = 0; i < tStrList.Count; ++i)
        {
            var ct = tStrList[i];
            contentStr = contentStr + ct + "\n";
            tStrList[i] = StringToBase64(ct);
        }
        System.IO.File.WriteAllLines(contentPath, tStrList.ToArray());

        Debug.Log("content_path:" + contentPath + "\n" + content);
    }

    public static string[] ReadLogContent(string key)
    {
        string[] contents = null;
        List<string> strList = new List<string>();
        var contentPath = LogContentPath(key);
        if (System.IO.File.Exists(contentPath))
        {
            var lines = System.IO.File.ReadAllLines(contentPath);
            foreach (var line in lines)
            {
                strList.Add(Base64ToString(line));
            }
            contents = strList.ToArray();
        }
        return contents;
    }

    public static string[] ReadLogKeys()
    {
        string[] keys = null;
        List<string> strList = new List<string>();
        if (System.IO.File.Exists(LogKeyPath))
        {
            var lines = System.IO.File.ReadAllLines(LogKeyPath);
            foreach (var line in lines)
            {
                strList.Add(Base64ToString(line));
            }
            keys = strList.ToArray();
        }
        return keys;
    }

    public static string Base64ToString(string str)
    {
        byte[] outputb = System.Convert.FromBase64String(str);
        return System.Text.Encoding.ASCII.GetString(outputb);
    }

    public static string StringToBase64(string str)
    {
        byte[] outputb = System.Text.Encoding.ASCII.GetBytes(str);
        return System.Convert.ToBase64String(outputb);
    }
}
